package com.tensquare.article.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(value = "thumbup/{articleId}", method = RequestMethod.PUT)
    public Result thumbup(@PathVariable String articleId) {
        String userId = "1";
        String key = "thumbup_article_" + userId + articleId;
        Object o = redisTemplate.opsForValue().get(key);
        if (o == null) {
            articleService.thumbup(articleId,userId);
            redisTemplate.opsForValue().set(key, 1);
            return new Result(true,StatusCode.OK,"点赞成功");
        }else {
            return new Result(true,StatusCode.OK,"不允许重复点赞");
        }
    }

    @RequestMapping(value = "/subscribe", method = RequestMethod.POST)
    public Result subscribe(@RequestBody Map map) {
        boolean flag = articleService.subscribe(map.get("articleid").toString(), map.get("userid").toString());
        if (flag == true) {
            return new Result(true, StatusCode.OK, "订阅成功");
        } else {
            return new Result(true, StatusCode.OK, "取消成功");
        }
    }


    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        // int i = 1/0;
        List<Article> article = articleService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", article);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result getById(@PathVariable String id) {
        Article article = articleService.getById(id);
        return new Result(true, StatusCode.OK, "查询成功", article);
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Article article) {
        articleService.add(article);
        return new Result(true, StatusCode.OK, "增加成功");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result updateById(@PathVariable String id, @RequestBody Article article) {
        article.setId(id);
        articleService.updateById(article);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        articleService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String, Object> map) {
        Page<Article> articlePage = articleService.findByPage(map, page, size);
        PageResult<Article> pageResult = new PageResult<>(articlePage.getTotal(),
                articlePage.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);

    }

}
